<template>
  <j-modal
    :title="title"
    :width="width"
    :visible="visible"
    :confirmLoading="confirmLoading"
    switchFullscreen
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭">
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">

        <a-form-item label="服务类型" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-select v-decorator="['serviceType']" placeholder="服务类型" disabled="true">
            <a-select-option :value="1">科学饮食</a-select-option>
            <a-select-option :value="2">睡眠指导</a-select-option>
            <a-select-option :value="3">运动禁忌</a-select-option>
            <a-select-option :value="4">健康学堂</a-select-option>
          </a-select>
        </a-form-item>

        <a-form-item label="文章内容" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-editor v-decorator="['articleContent']" placeholder="请输入内容" triggerChange />
        </a-form-item>

      </a-form>
    </a-spin>
  </j-modal>
</template>

<script>

  import {httpAction, getAction} from '@/api/manage'
  import pick from 'lodash.pick'
  import {validateDuplicateValue} from '@/utils/util'
  import JDate from '@/components/jeecg/JDate'
  import JEditor from '@/components/jeecg/JEditor'


  export default {
    name: "PusGroupServiceArticleModal",
    components: {
      JDate,
      JEditor,
    },
    data() {
      return {
        form: this.$form.createForm(this),
        title: "操作",
        width: 800,
        visible: false,
        model: {},
        labelCol: {
          xs: {span: 24},
          sm: {span: 5},
        },
        wrapperCol: {
          xs: {span: 24},
          sm: {span: 16},
        },
        confirmLoading: false,
        validatorRules: {
          groupId: {
            rules: [
              {required: true, message: '请输入圈子id!'},
            ]
          },
        },
        url: {
          add: "/group/pusGroupServiceArticle/add",
          edit: "/group/pusGroupServiceArticle/edit",
          getOneByType: "/group/pusGroupServiceArticle/getOneByType",
        }
      }
    },
    created() {
    },
    methods: {
      detail(record,type) {
        this.form.resetFields();
        //查询圈子的类型信息
        let param = {groupId : record.id ,serviceType : type}
        getAction(this.url.getOneByType, param).then((res) => {
          if (res.success) {
            let obj = res.result;
            this.form.resetFields();
            this.model = Object.assign({}, obj);
            this.$nextTick(() => {
              this.form.setFieldsValue(pick(this.model, 'serviceType', 'groupId', 'articleContent', 'createTime', 'createBy', 'updateTime', 'updateBy', 'delFlag'))
            })
          } else {
            this.$message.warning(res.message);
          }
        })
        this.visible = true;
      },
      close() {
        this.$emit('close');
        this.visible = false;
      },
      handleOk() {
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true;
            let httpUrl = '';
            let method = '';
            if (!this.model.id) {
              httpUrl += this.url.add;
              method = 'post';
            } else {
              httpUrl += this.url.edit;
              method = 'put';
            }
            let formData = Object.assign(this.model, values);
            console.log("表单提交数据", formData)
            httpAction(httpUrl, formData, method).then((res) => {
              if (res.success) {
                that.$message.success(res.message);
                that.$emit('ok');
              } else {
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
              that.close();
            })
          }

        })
      },
      handleCancel() {
        this.close()
      },
      popupCallback(row) {
        this.form.setFieldsValue(pick(row, 'serviceType', 'groupId', 'articleContent', 'createTime', 'createBy', 'updateTime', 'updateBy', 'delFlag'))
      },


    }
  }
</script>